package com.bjpowernode.listener;

import com.bjpowernode.POJO.Admin;
import com.bjpowernode.POJO.ProductType;
import com.bjpowernode.service.ProductTypeService;
import com.bjpowernode.service.impl.ProductTypeServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import java.util.List;

@WebListener
public class ProductTypeListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        // 手工从Spring 容器中，获取ProductTypeServiceImpl 的对象
        ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext_*.xml");
        ProductTypeService productTypeServiceImpl =(ProductTypeService) app.getBean("productTypeServiceImpl");
        List<ProductType> all = productTypeServiceImpl.getAll();

        // 放入全局应用作用域中，供新增页面，修改页面，前台的查询工能提供全部商品类别集合
        servletContextEvent.getServletContext().setAttribute("ptlist",all);
    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {

    }

    private void test(Object o){
    }
}
